home *** CD-ROM | disk | FTP | other *** search
/ NetObjects Fusion 7 / Fusion7.iso / NetObjects Fusion / data1.cab / Language_Resource_-_English / Components / TimeBasedApplet / TimeBased.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-10-18  |  8.7 KB  |  262 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Rectangle;
  10. import java.awt.image.ImageObserver;
  11. import java.net.URL;
  12. import java.util.Date;
  13.  
  14. public class TimeBased extends Applet implements Runnable {
  15.    Thread m_TimeBased = null;
  16.    static final String STRING_Error_forming_URL = new String("Error forming URL for background image");
  17.    static final String STRING_Image_loading_interrupted = new String("Background image loading interrupted");
  18.    static final String STRING_Unable_to_load_images = new String("Unable to load images over Windows network!");
  19.    static final String STRING_Loading_images = new String("Loading images...");
  20.    static final String STRING_Error_loading_images = new String("Error loading images!");
  21.    static final String STRING_Bad_URL = new String("Bad URL:");
  22.    private Graphics m_Graphics;
  23.    private Image[] m_Images;
  24.    private int m_nCurrImage;
  25.    private int m_nImgWidth = 0;
  26.    private int m_nImgHeight = 0;
  27.    private boolean m_fAllLoaded = false;
  28.    private boolean NetworkFiles = false;
  29.    private boolean didClick;
  30.    private int numImages;
  31.    private String[] imageNames;
  32.    private String[] imageURLs;
  33.    private int[] imageStarts;
  34.    private MediaTracker tracker;
  35.    protected Color backgroundColor = null;
  36.    protected Image backgroundImage = null;
  37.    protected int xOffset;
  38.    protected int yOffset;
  39.  
  40.    public String getAppletInfo() {
  41.       return "Name: TimeBased\r\n";
  42.    }
  43.  
  44.    public void init() {
  45.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  46.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  47.       this.didClick = false;
  48.       this.numImages = Integer.parseInt(((Applet)this).getParameter("Number of Images"));
  49.       this.imageNames = new String[this.numImages];
  50.       this.imageURLs = new String[this.numImages];
  51.       this.imageStarts = new int[this.numImages];
  52.  
  53.       for(int cnt = 0; cnt < this.numImages; ++cnt) {
  54.          this.imageNames[cnt] = ((Applet)this).getParameter("Image " + (cnt + 1));
  55.          if (this.imageNames[cnt] != null && this.imageNames[cnt].startsWith("file://///")) {
  56.             this.NetworkFiles = true;
  57.          }
  58.  
  59.          this.imageURLs[cnt] = ((Applet)this).getParameter("URL for Image " + (cnt + 1));
  60.          if (this.imageURLs[cnt] != null) {
  61.             if (this.imageURLs[cnt].startsWith(".")) {
  62.                String theBase = ((Applet)this).getDocumentBase().toExternalForm();
  63.                int lastSlash = theBase.lastIndexOf(47);
  64.                this.imageURLs[cnt] = theBase.substring(0, lastSlash + 1) + this.imageURLs[cnt];
  65.             } else if (this.imageURLs[cnt].compareTo("javascript:void(0)") == 0) {
  66.                this.imageURLs[cnt] = null;
  67.             } else if (!this.imageURLs[cnt].startsWith("cid:") && !this.imageURLs[cnt].startsWith("lifn:") && !this.imageURLs[cnt].startsWith("java:") && !this.imageURLs[cnt].startsWith("irc:") && !this.imageURLs[cnt].startsWith("IOR:") && !this.imageURLs[cnt].startsWith("ilu:") && !this.imageURLs[cnt].startsWith("https:") && !this.imageURLs[cnt].startsWith("http:") && !this.imageURLs[cnt].startsWith("hdl:") && !this.imageURLs[cnt].startsWith("gopher:") && !this.imageURLs[cnt].startsWith("ftp:") && !this.imageURLs[cnt].startsWith("finger:") && !this.imageURLs[cnt].startsWith("file:") && !this.imageURLs[cnt].startsWith("data:") && !this.imageURLs[cnt].startsWith("clsid:") && !this.imageURLs[cnt].startsWith("md5:") && !this.imageURLs[cnt].startsWith("mailserver:") && !this.imageURLs[cnt].startsWith("mailto:") && !this.imageURLs[cnt].startsWith("mid:") && !this.imageURLs[cnt].startsWith("news:") && !this.imageURLs[cnt].startsWith("nntp:") && !this.imageURLs[cnt].startsWith("path:") && !this.imageURLs[cnt].startsWith("prospero:") && !this.imageURLs[cnt].startsWith("service:") && !this.imageURLs[cnt].startsWith("shttp") && !this.imageURLs[cnt].startsWith("snews") && !this.imageURLs[cnt].startsWith("STANF:") && !this.imageURLs[cnt].startsWith("telnet:") && !this.imageURLs[cnt].startsWith("vemmi:") && !this.imageURLs[cnt].startsWith("wais:") && !this.imageURLs[cnt].startsWith("whois++:")) {
  68.                this.imageURLs[cnt] = "http://" + this.imageURLs[cnt];
  69.             }
  70.          }
  71.  
  72.          this.imageStarts[cnt] = Integer.parseInt(((Applet)this).getParameter("Start Time for Image " + (cnt + 1)));
  73.       }
  74.  
  75.       if (((Applet)this).getParameter("BackgroundColor") != null) {
  76.          int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  77.          backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  78.          this.backgroundColor = new Color(backgroundcolor);
  79.          ((Component)this).setBackground(this.backgroundColor);
  80.       } else if (((Applet)this).getParameter("BackgroundImage") != null) {
  81.          String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  82.  
  83.          try {
  84.             this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  85.          } catch (Exception var7) {
  86.             System.out.println(STRING_Error_forming_URL);
  87.             return;
  88.          }
  89.  
  90.          MediaTracker tracker = new MediaTracker(this);
  91.          tracker.addImage(this.backgroundImage, 0);
  92.  
  93.          try {
  94.             tracker.waitForID(0);
  95.          } catch (InterruptedException var6) {
  96.             System.out.println(STRING_Image_loading_interrupted);
  97.          }
  98.       }
  99.  
  100.    }
  101.  
  102.    private int convertBGRtoRGB(int BGRColor) {
  103.       int r = (BGRColor & 255) << 16;
  104.       int g = BGRColor & '\uff00';
  105.       int b = (BGRColor & 16711680) >> 16;
  106.       return r + g + b;
  107.    }
  108.  
  109.    private String modifyStringContext(String param) {
  110.       if (param.startsWith(".")) {
  111.          param = param.replace('\\', '/');
  112.          String fullBase = ((Applet)this).getDocumentBase().toString();
  113.          param = fullBase.substring(0, fullBase.lastIndexOf(47)) + "/" + param;
  114.       } else if (param.startsWith("#")) {
  115.          param = ((Applet)this).getDocumentBase().toString() + param;
  116.       } else if (param.compareTo("javascript:void(0)") == 0) {
  117.          param = null;
  118.       } else if (!param.startsWith("cid:") && !param.startsWith("lifn:") && !param.startsWith("java:") && !param.startsWith("irc:") && !param.startsWith("IOR:") && !param.startsWith("ilu:") && !param.startsWith("https:") && !param.startsWith("http:") && !param.startsWith("hdl:") && !param.startsWith("gopher:") && !param.startsWith("ftp:") && !param.startsWith("finger:") && !param.startsWith("file:") && !param.startsWith("data:") && !param.startsWith("clsid:") && !param.startsWith("md5:") && !param.startsWith("mailserver:") && !param.startsWith("mailto:") && !param.startsWith("mid:") && !param.startsWith("news:") && !param.startsWith("nntp:") && !param.startsWith("path:") && !param.startsWith("prospero:") && !param.startsWith("service:") && !param.startsWith("shttp") && !param.startsWith("snews") && !param.startsWith("STANF:") && !param.startsWith("telnet:") && !param.startsWith("vemmi:") && !param.startsWith("wais:") && !param.startsWith("whois++:")) {
  119.          param = "http://" + param;
  120.       }
  121.  
  122.       return param;
  123.    }
  124.  
  125.    public void destroy() {
  126.    }
  127.  
  128.    private void displayImage(Graphics g) {
  129.       if (this.m_fAllLoaded) {
  130.          if (this.backgroundImage != null) {
  131.             int imHeight = this.backgroundImage.getHeight(this);
  132.             int imWidth = this.backgroundImage.getWidth(this);
  133.             Dimension d = ((Component)this).size();
  134.             int x = (d.width + this.xOffset) / imWidth + 1;
  135.             int y = (d.height + this.yOffset) / imHeight + 1;
  136.  
  137.             for(int i = 0; i < y; ++i) {
  138.                for(int j = 0; j < x; ++j) {
  139.                   g.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  140.                }
  141.             }
  142.          } else {
  143.             g.clearRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  144.          }
  145.  
  146.          g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_Images[this.m_nCurrImage].getWidth(this)) / 2, (((Component)this).size().height - this.m_Images[this.m_nCurrImage].getHeight(this)) / 2, (ImageObserver)null);
  147.       }
  148.    }
  149.  
  150.    public void paint(Graphics g) {
  151.       if (this.m_fAllLoaded) {
  152.          Rectangle r = g.getClipRect();
  153.          g.clearRect(r.x, r.y, r.width, r.height);
  154.          this.displayImage(g);
  155.       } else if (this.NetworkFiles) {
  156.          g.drawString(STRING_Unable_to_load_images, 10, 20);
  157.       } else {
  158.          g.drawString(STRING_Loading_images, 10, 20);
  159.       }
  160.  
  161.    }
  162.  
  163.    public void start() {
  164.       if (this.m_TimeBased == null) {
  165.          this.m_TimeBased = new Thread(this);
  166.          this.m_TimeBased.start();
  167.       }
  168.  
  169.    }
  170.  
  171.    public void stop() {
  172.       if (this.m_TimeBased != null) {
  173.          this.m_TimeBased.stop();
  174.          this.m_TimeBased = null;
  175.       }
  176.  
  177.    }
  178.  
  179.    public void run() {
  180.       Date today = new Date(System.currentTimeMillis());
  181.       int currentHours = today.getHours();
  182.  
  183.       for(this.m_nCurrImage = 0; this.m_nCurrImage < this.numImages - 1 && this.imageStarts[this.m_nCurrImage + 1] <= currentHours; ++this.m_nCurrImage) {
  184.       }
  185.  
  186.       if (!this.m_fAllLoaded) {
  187.          ((Component)this).repaint();
  188.          this.m_Graphics = ((Component)this).getGraphics();
  189.          this.m_Images = new Image[this.numImages];
  190.          this.tracker = new MediaTracker(this);
  191.  
  192.          for(int i = 1; i <= this.numImages; ++i) {
  193.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), this.imageNames[i - 1]);
  194.             this.tracker.addImage(this.m_Images[i - 1], i - 1);
  195.          }
  196.  
  197.          try {
  198.             this.tracker.waitForID(this.m_nCurrImage);
  199.             this.m_fAllLoaded = !this.tracker.isErrorID(this.m_nCurrImage);
  200.          } catch (InterruptedException var5) {
  201.          }
  202.  
  203.          if (!this.m_fAllLoaded) {
  204.             this.m_Graphics.drawString(STRING_Error_loading_images, 10, 40);
  205.             this.stop();
  206.             return;
  207.          }
  208.       }
  209.  
  210.       ((Component)this).repaint();
  211.  
  212.       while(true) {
  213.          try {
  214.             this.displayImage(this.m_Graphics);
  215.             today = new Date(System.currentTimeMillis());
  216.             currentHours = today.getHours();
  217.  
  218.             for(this.m_nCurrImage = 0; this.m_nCurrImage < this.numImages - 1 && this.imageStarts[this.m_nCurrImage + 1] <= currentHours; ++this.m_nCurrImage) {
  219.             }
  220.  
  221.             this.tracker.waitForID(this.m_nCurrImage);
  222.             this.m_fAllLoaded = !this.tracker.isErrorID(this.m_nCurrImage);
  223.             if (!this.m_fAllLoaded) {
  224.                this.stop();
  225.                this.m_Graphics.drawString(STRING_Error_loading_images, 10, 40);
  226.                return;
  227.             }
  228.  
  229.             Thread.sleep(300000L);
  230.          } catch (InterruptedException var6) {
  231.             this.stop();
  232.          }
  233.       }
  234.    }
  235.  
  236.    public boolean mouseDown(Event evt, int x, int y) {
  237.       this.didClick = true;
  238.       return true;
  239.    }
  240.  
  241.    public boolean mouseUp(Event evt, int x, int y) {
  242.       if (this.didClick) {
  243.          try {
  244.             ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]));
  245.          } catch (Exception var6) {
  246.             ((Applet)this).getAppletContext().showStatus(STRING_Bad_URL + this.imageURLs[this.m_nCurrImage]);
  247.          }
  248.       }
  249.  
  250.       return true;
  251.    }
  252.  
  253.    public boolean mouseEnter(Event evt, int x, int y) {
  254.       return true;
  255.    }
  256.  
  257.    public boolean mouseExit(Event evt, int x, int y) {
  258.       this.didClick = false;
  259.       return true;
  260.    }
  261. }
  262.